<?php

namespace app\admin\controller;
use app\admin\model\MemberModel;
use think\Db;

class Member extends Base
{

    //*********************************************会员列表*********************************************//
    /**
     * 会员列表
     * @author [田建龙] [864491238@qq.com]
     */
    public function index(){

        $key = input('key');
        $level_name = input('level_name');  //商品分类
        $role = input('role');  //商品分类
        $phone = input('phone');  //商品分类
        //$map['closed'] = 0;//0未删除，1已删除
        $map= [];//0未删除，1已删除
        if($key&&$key!=="")
        {
           // $map['account|nickname|mobile'] = ['like',"%" . $key . "%"];
            $map['nickname'] = ['like',"%" . $key . "%"];
        }

        if($level_name&&$level_name!==""){
            $map['level_name'] = ['like',"%" . $level_name . "%"];
        }

        if($phone&&$phone!==""){
            $map['phone'] = $phone;
        }

        $member = new MemberModel();       
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = $member->getAllCount($map);//计算总页面
        $allpage = intval(ceil($count / $limits));       
        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);   
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('level_name', $level_name);
        $this->assign('count', $count);
        $this->assign('role',$role);
        $this->assign('phone',$phone);

        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }


    /**
     * 添加会员
     * @author [田建龙] [864491238@qq.com]
     */
    /*public function add_member()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $param['password'] = md5(md5($param['password']) . config('auth_key'));
            $member = new MemberModel();
            $flag = $member->insertMember($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $group = new MemberGroupModel();
        $this->assign('group',$group->getGroup());
        return $this->fetch();
    }*/


    /**
     * 编辑会员
     * @author [田建龙] [864491238@qq.com]
     */
    /*public function edit_member()
    {
        $member = new MemberModel();
        if(request()->isAjax()){
            $param = input('post.');
            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5(md5($param['password']) . config('auth_key'));
            }
            $flag = $member->editMember($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $group = new MemberGroupModel();
        $this->assign([
            'member' => $member->getOneMember($id),
            'group' => $group->getGroup()
        ]);
        return $this->fetch();
    }*/


    /**
     * 删除会员
     * @author [田建龙] [864491238@qq.com]
     */
    /*public function del_member()
    {
        $id = input('param.id');
        $member = new MemberModel();
        $flag = $member->delMember($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }*/



    /**
     * 会员状态
     * @author [田建龙] [864491238@qq.com]
     */
    /*public function member_status()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }*/

    /**
     * @return [type] [description]
     * @author felix
     */
    public function goods_new_state()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id',$id)->value('is_shoper');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('member')->where('id',$id)->update(['is_shoper'=>-1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '切换会员成功']);
        }
        else
        {
            $flag = Db::name('member')->where('id',$id)->update(['is_shoper'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '切换店员成功']);
        }
    }



    public function goods_new_state1()
    {
        $id = input('param.id');
        $openid = input('param.openid');
        if(!$openid) return json(['code' => 0, 'data' => [], 'msg' => '请输入OPENID']);
        $status = Db::name('member')->where('id',$id)->update(['official_openid'=>$openid]);//判断当前状态情况
        if($status)
        {
            return json(['code' => 1, 'data' => [], 'msg' => '成功']);
        }
        else
        {;
            return json(['code' => 0, 'data' => [], 'msg' => '失败']);
        }
    }




}